﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MM.Commmon.UI;
using MM.Commmon.UI.Controls;
using DevExpress.XtraBars.Docking;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList;
using System.Data;
using MM.Common.Base;
using MM.Common.DBBase;
using DevExpress.XtraTreeList.Nodes;
using System.Drawing;
using DevExpress.XtraEditors.Controls;
using System.Drawing.Drawing2D;

namespace MM.Common.Support
{
    public partial class FrmEmpRightsSearch : FrmFunctionBase
    {
        public FrmEmpRightsSearch()
        {
            InitializeComponent();
        }

        private void SearchByName()
        {
            this.Add(this.rgName.EditValue.ToString(), true);
        }

        private void SearchByRole()
        {
            string str2 = "'";
            string str = this.rgRole.EditValue.ToString();
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] != ' ')
                {
                    if (str[i] == ',')
                    {
                        str2 = (str2 + "'") + (Convert.ToString(str[i])) + "'";
                    }
                    else
                    {
                        str2 = str2 + str[i];
                    }
                }
            }
            str2 = str2 + "'";
            string commandtext = "SELECT DISTINCT EMP_ID FROM MXHLY_SYS_ROLE_EMP WHERE ROLE_CODE IN (" + str2 + ")";
            DataTable dt = new DataTable();
            SystemInfo.DbData.Retrieve(commandtext, ref dt);
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                this.Add(dt.Rows[j][0].ToString(), false);
            }
        }

        private void FrmEmpRightsSearch_Load(object sender, EventArgs e)
        {
            this.rgName.ShowTab = "MXH_EMPLOYEE";
            this.rgName.ShowTabText = "EMP_NAME";
            this.rgName.ShowTabValue = "EMP_ID";
            this.rgName.ShowTabID = "EMP_CODE";
            this.rgName.ShowTabCode = "INPUTCODE";
            this.rgName.setListValue();
            this.rgRole.ShowTab = "MXHLY_SYS_ROLE";
            this.rgRole.ShowTabText = "ROLE_NAME";
            this.rgRole.ShowTabValue = "ROLE_CODE";
            this.rgRole.ShowTabID = "EMP_CODE";
            this.rgRole.setListValue();
        }

        private void Add(string empid, bool isShowFun)
        {
            this.treeList.BeginUpdate();
            string commandtext = "SELECT ORDER_NO,TITLE FROM MXHLY_SYS_MENU WHERE FUNCTION_CODE IS NULL ORDER BY ORDER_NO";
            DataTable dt = new DataTable();
            SystemInfo.DbData.Retrieve(commandtext, ref dt);
            DataTable table2 = new DataTable();
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("empid", empid) });
            OutputDataParam dataParam = new OutputDataParam("rescursor", Type.GetType("System.Boolean"));
            parms.Add(dataParam);
            SystemInfo.DbData.RetrieveProcedure("P_KTSUPPORT_RIGHT", parms, ref table2);
            if (table2.Rows.Count > 0)
            {
                TreeListNode parentNode = this.treeList.AppendNode(table2.Rows[0], -1);
                parentNode.SelectImageIndex = 0;
                parentNode.ImageIndex = 0;
                if (isShowFun)
                {
                    commandtext = "SELECT ORDER_NO,TITLE FROM MXHLY_SYS_MENU WHERE FUNCTION_CODE IN (SELECT FUNCTION_CODE FROM MXHLY_SYS_FUNCTION_ROLE WHERE ROLE_CODE IN (SELECT ROLE_CODE FROM MXHLY_SYS_ROLE_EMP WHERE EMP_ID = :emp_id)) ORDER BY ORDER_NO";
                    DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("emp_id", empid) });
                    DataTable table3 = new DataTable();
                    SystemInfo.DbData.Retrieve(commandtext, parm, ref table3);
                    int[] numArray = new int[dt.Rows.Count];
                    int[] numArray2 = new int[dt.Rows.Count];
                    int[] numArray3 = new int[table3.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        numArray[i] = 0;
                        numArray2[i] = -1;
                    }
                    for (int j = 0; j < table3.Rows.Count; j++)
                    {
                        string child = (string)table3.Rows[j][0];
                        for (int num3 = 0; num3 < dt.Rows.Count; num3++)
                        {
                            string parent = (string)dt.Rows[num3][0];
                            if (this.CheckRelation(child, parent))
                            {
                                numArray3[j] = num3;
                                numArray[num3]++;
                            }
                        }
                    }
                    for (int k = 0; k < dt.Rows.Count; k++)
                    {
                        if (numArray[k] != 0)
                        {
                            string str4 = (string)dt.Rows[k][0];
                            for (int num5 = 0; num5 < dt.Rows.Count; num5++)
                            {
                                if (k != num5)
                                {
                                    string str5 = (string)dt.Rows[num5][0];
                                    if ((str4.Length > str5.Length) && this.CheckRelation(str4, str5))
                                    {
                                        numArray2[k] = num5;
                                        numArray[num5]++;
                                    }
                                }
                            }
                        }
                    }
                    TreeListNode[] nodeArray = new TreeListNode[dt.Rows.Count];
                    dt.Columns.Remove(dt.Columns[0]);
                    for (int m = 0; m < dt.Rows.Count; m++)
                    {
                        if (numArray[m] > 0)
                        {
                            if (numArray2[m] == -1)
                            {
                                nodeArray[m] = this.treeList.AppendNode(dt.Rows[m], parentNode);
                            }
                            else
                            {
                                nodeArray[m] = this.treeList.AppendNode(dt.Rows[m], nodeArray[numArray2[m]]);
                            }
                            nodeArray[m].SelectImageIndex = 1;
                            nodeArray[m].ImageIndex = 1;
                        }
                    }
                    table3.Columns.Remove(table3.Columns[0]);
                    for (int n = 0; n < table3.Rows.Count; n++)
                    {
                        TreeListNode node2 = this.treeList.AppendNode(table3.Rows[n], nodeArray[numArray3[n]]);
                        node2.SelectImageIndex = 2;
                        node2.ImageIndex = 2;
                    }
                    parentNode.ExpandAll();
                }
            }
            this.treeList.EndUpdate();
        }

        private void BtSearch_Click(object sender, EventArgs e)
        {
            this.treeList.ClearNodes();
            if (Convert.IsDBNull(this.rgName.EditValue))
            {
                this.SearchByRole();
            }
            else
            {
                this.SearchByName();
            }
        }

        private bool CheckRelation(string child, string parent)
        {
            return (child.Substring(0, child.Length - 1) == parent);
        }
    }
}
